﻿namespace FiftyOne.Foundation.Mobile.Detection.Wurfl.Matchers.Version
{
    using FiftyOne.Foundation.Mobile.Detection.Wurfl;
    using FiftyOne.Foundation.Mobile.Detection.Wurfl.Handlers;
    using FiftyOne.Foundation.Mobile.Detection.Wurfl.Matchers;
    using System;
    using System.Collections.Generic;
    using System.Runtime.InteropServices;

    internal class Matcher
    {
        internal static Results Match(string userAgent, VersionHandler handler)
        {
            string str2 = null;
            int num2;
            int[] numArray = new int[handler.VersionRegexes.Length];
            List<DeviceResult> list = new List<DeviceResult>(handler.UserAgents.Count);
            Results results = new Results();
            int num = 0x7fffffff;
            foreach (DeviceInfo[] infoArray in handler.UserAgents)
            {
                foreach (DeviceInfo info in infoArray)
                {
                    SegmentScore[] scores = new SegmentScore[handler.VersionRegexes.Length];
                    num2 = 0;
                    while (num2 < handler.VersionRegexes.Length)
                    {
                        str2 = handler.VersionRegexes[num2].Match(userAgent).Value;
                        string str = handler.VersionRegexes[num2].Match(info.UserAgent).Value;
                        for (int i = 0; (i < str2.Length) && (i < str.Length); i++)
                        {
                            scores[num2].Difference += Math.Abs((int) (str2[i] - str[i]));
                            scores[num2].CharactersMatched++;
                        }
                        if (scores[num2].CharactersMatched > numArray[num2])
                        {
                            numArray[num2] = scores[num2].CharactersMatched;
                        }
                        num2++;
                    }
                    list.Add(new DeviceResult(scores, info));
                }
            }
            foreach (DeviceResult result in list)
            {
                int num4 = 0;
                for (num2 = 0; num2 < handler.VersionRegexes.Length; num2++)
                {
                    num4 += ((numArray[num2] - result.Scores[num2].CharactersMatched) + 1) * ((result.Scores[num2].Difference + numArray[num2]) - result.Scores[num2].CharactersMatched);
                }
                if (num4 < num)
                {
                    results.Clear();
                    num = num4;
                }
                if (num4 == num)
                {
                    results.Add(result.Device);
                }
            }
            return results;
        }

        private class DeviceResult
        {
            internal DeviceInfo Device = null;
            internal FiftyOne.Foundation.Mobile.Detection.Wurfl.Matchers.Version.Matcher.SegmentScore[] Scores = null;

            internal DeviceResult(FiftyOne.Foundation.Mobile.Detection.Wurfl.Matchers.Version.Matcher.SegmentScore[] scores, DeviceInfo device)
            {
                this.Scores = scores;
                this.Device = device;
            }
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct SegmentScore
        {
            internal int CharactersMatched;
            internal int Difference;
        }
    }
}

